package xyz.xpman.prisonerfitness.entity;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("unused")
public class TrainingStandard {
    public static final String EASY = "easy";
    public static final String MEDIUM = "medium";
    public static final String UPGRADE = "upgrade";
    public static final String CUSTOM = "custom";

    private final Map<String, Standard> standard = new HashMap<>();



    public Standard getEasyStandard() {
        return standard.get(EASY);
    }

    public void setEasyStandard(int times) {
        setStandard(EASY, times);
    }

    public void setEasyStandard(int group, int times) {
        setStandard(EASY, group, times);
    }

    private void setEasyStandard(@NotNull Standard standard) {
        setStandard(EASY, standard);
    }



    public Standard getMediumStandard() {
        return standard.get(MEDIUM);
    }

    public void setMediumStandard(int times) {
        setStandard(MEDIUM, times);
    }

    public void setMediumStandard(int group, int times) {
        setStandard(MEDIUM, group, times);
    }

    private void setMediumStandard(@NotNull Standard standard) {
        setStandard(MEDIUM, standard);
    }


    public Standard getUpgradeStandard() {
        return standard.get(UPGRADE);
    }

    public void setUpgradeStandard(int times) {
        setStandard(UPGRADE, times);
    }

    public void setUpgradeStandard(int group, int times) {
        setStandard(UPGRADE, group, times);
    }

    private void setUpgradeStandard(@NotNull Standard standard) {
        setStandard(UPGRADE, standard);
    }


    public CustomStandard getCustomStandard() {
        return (CustomStandard) standard.get(CUSTOM);
    }

    public void setCustomStandard(int times) {
        setStandard(CUSTOM, times);
    }

    public void setCustomStandard(int group, int times) {
        setStandard(CUSTOM, group, times);
    }

    private void setCustomStandard(@NotNull CustomStandard standard) {
        setStandard(CUSTOM, standard);
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    private void setStandard(@NotNull String name, int times) {
        this.standard.put(name, new Standard(times));
    }

    private void setStandard(@NotNull String name, int group, int times) {
        this.standard.put(name, new Standard(group, times));
    }

    private void setStandard(@NotNull String name, @NotNull Standard standard) {
        this.standard.put(name, standard);
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    public static TrainingStandard byJson(@NotNull JSONObject json) {
        TrainingStandard training = new TrainingStandard();

        try {

            training.setEasyStandard(Standard.byJson(json.getJSONObject(EASY)));
            training.setMediumStandard(Standard.byJson(json.getJSONObject(MEDIUM)));
            training.setUpgradeStandard(Standard.byJson(json.getJSONObject(UPGRADE)));


            CustomStandard customStandard = json.has(CUSTOM)
                    ? CustomStandard.byJson(json.getJSONObject(CUSTOM))
                    : new CustomStandard(training.getUpgradeStandard());

            training.setCustomStandard(customStandard);


        } catch (JSONException e) {
            e.printStackTrace();
        }

        return training;
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    public static class Standard extends Record implements IStandard {
        public static final String GROUP = "group";
        public static final String TIMES = "times";

        public Standard (int times) {
            this(1, times);
        }

        public Standard (int group, int times) {
            set(GROUP, group);
            set(TIMES, times);
        }


        public int getGroups() {
            return getInt(GROUP);
        }

        public int getTimes() {
            return getInt(TIMES);
        }


        public static Standard byJson(@NotNull JSONObject json) {
            Standard standard = null;
            try {
                int group = json.getInt(GROUP);
                int times = json.getInt(TIMES);
                standard = new Standard(group, times);

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return standard;
        }

        @Override
        public @NotNull String toString() {
            return getGroups() + "组 " + getTimes() + "次";
        }
    }


    public static class CustomStandard extends Standard {

        public CustomStandard(@NotNull IStandard standard) {
            this(standard.getGroups(), standard.getTimes());
        }

        public CustomStandard(int times) {
            super(times);
        }

        public CustomStandard(int group, int times) {
            super(group, times);
        }


        public CustomStandard setGroup(int group) {
            set(GROUP, group);
            return this;
        }

        public CustomStandard setTimes(int times) {
            set(TIMES, times);
            return this;
        }


        public static CustomStandard byJson(@NotNull JSONObject json) {
            return new CustomStandard(Standard.byJson(json));
        }
    }


    public interface IStandard extends Serializable {

        /**
         * 获取训练组数
         * @return 训练组数
         */
        int getGroups();

        /**
         * 获取训练次数
         * @return 训练次数
         */
        int getTimes();

    }
}
